package com.fh.config.security;


import com.alibaba.fastjson2.JSON;
import com.fh.bean.pulc.ResultBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Autoor:whf
 * 当用户登录后,访问接口没有权限的时候会被调用
 */

@Slf4j
public class CustomAccessDeniedHandlerConfig implements AccessDeniedHandler {


    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
        log.info("抱歉，您没有访问该接口的权限???");
        ResultBody<Object> retResult = ResultBody.error("抱歉，您没有访问该接口的权限");
        httpServletResponse.setContentType("application/json;charset=utf-8");
        httpServletResponse.setCharacterEncoding("UTF-8");
        httpServletResponse.getWriter().write(JSON.toJSONString(retResult));
    }
}